<template>
    <Form ref="formInline" :model="formInline" :rules="ruleInline">
        <FormItem prop="user">
            <Input type="text" v-model="formInline.userName" placeholder="userName">
                <Icon type="ios-person-outline" slot="prepend"></Icon>
            </Input>
        </FormItem>
            <Input type="password" v-model="formInline.password" placeholder="Password">
                <Icon type="ios-locked-outline" slot="prepend"></Icon>
            </Input>
        </FormItem>
        <FormItem>
            <Button type="primary" @click="handleSubmit('formInline')">登录</Button>
        </FormItem>
    </Form>
</template>

<script>
    import {mapState, mapMutations} from 'vuex'
    import {accountLogin} from '../../service/getData'

    export default {
        data(){
            return {
                formInline: {
                    userName: '',
                    password: ''
                },
                ruleInline: {
                    userName: [
                        { required: true, message: '请填写用户名', trigger: 'blur' }
                    ],
                    password: [
                        { required: true, message: '请填写密码', trigger: 'blur' },
                        { type: 'string', min: 6, message: '密码长度不能小于6位', trigger: 'blur' }
                    ]
                }
            }
        },
        methods: {
            ...mapMutations([
                'RECORD_USERINFO',
            ]),
            handleSubmit(name) {
                this.$refs[name].validate((valid) => {
                    if (valid) {
                        this.userLogin()
                    } else {
                        this.$Message.error('表单验证失败!')
                    }
                })
            },
            //发送登录信息
            async userLogin(){
                //用户名登录
                this.userInfo = await accountLogin(this.formInline.userName, this.formInline.password)
                this.RECORD_USERINFO(this.userInfo);
            }
        }
    }

</script>
